package com.example.shopping.dao;

import com.example.shopping.pojo.Specification;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.List;


/*
Create by 苍狼 于 2020/1/16
站在巅峰看世界，
落在深渊思人生。
*/
@Repository
//规格
public interface ISpecificationDao {
    //规格全部查询
    @Select("select s.*,o.* from specification as s inner join specification_option as o on s.copy_id=o.spec_id ")
    @Results(id = "selectSpecification", value = {
//            @Result(column = "s.id",property = "id",javaType = Specification.class,id = true),
//            @Result(column = "s.spec_name",property = "spec_name"),
//            @Result(column = "o.id",property = "specificationOptionList.specificationOption.id"),
//            @Result(column = "o.option_name",property = "specificationOptionList.specificationOption.option_name"),
//            @Result(column = "o.spec_id",property = "specificationOptionList.specificationOption.spec_id"),
            @Result(column = "copy_id", property = "copyId"),
            @Result(column = "copy_id", property = "specificationOptionList", many = @Many(select = "com.example.shopping.dao.ISpecificationOptionDao.selectListSpecificationOptionByCopyId", fetchType = FetchType.EAGER)),
    })
    List<Specification> selectSpecification();

    //新增规格
    //
    @Insert("insert into  specification(id,spec_name,copy_id) values(null,#{specName},#{copyId})")
    //通过获取id关联进行新增  这里我新增暂时拆开来做，在新增之后再次弹出框，获取关联id，进行下一步的添加
    @Options(useGeneratedKeys = true, keyColumn = "copy_id")
    int insertSpecification(Specification specification);

    //删除时先查询这条信息的外键，找到之后，传给规格详细信息表，进行删除
    @Delete("delete from specification where id=#{id}")
    int deleteSpecification(int id);

    //根据规格名称模糊查询
    @Select("select * from specification where spec_name = #{specName}")
    @ResultMap("selectSpecification")
    List<Specification> quertByLikespecName(String specName);

    //查询单个，用于进行查询修改单条数据
    @Select("select * from specification where id=#{id}")
    @ResultMap("selectSpecification")
    Specification selectByOneSpecification(int id);

    //修改方法
    @Update("update specification set spec_name =#{specName} where id =#{id}")
    @ResultMap("selectSpecification")
    int updateSpecificationById(int id);


}
